package bin_tree.oj;

public class Num606 {
    StringBuilder sb = new StringBuilder();
    public String tree2str(TreeNode root) {
        if(root == null){
            return "";
        }
        preOrder(root);
        return sb.toString();
    }
    private void preOrder(TreeNode root){
        if(root == null){
            return;
        }
        sb.append(root.val);
        if(root.left != null){
            sb.append("(");
            preOrder(root.left);
            sb.append(")");
        }else {
            if (root.right != null){
                sb.append("()");
            }
        }
        if (root.right != null){
            sb.append("(");
            preOrder(root.right);
            sb.append(")");
        }
    }
}
